<template>
    <view>
        <form @submit="add_submitFn">
            <view class="addbox">
                <textarea :autoFocus="true" @input="contentFn" maxlength="141" name="content" placeholder="请输入内容，比如您的三观、心情、近期心愿等......"></textarea>
                <view class="h4">
                    <text class="span">{{ ic }}</text>
                    /140
                </view>
                <view class="ul">
                    <view @tap="upFn" class="li">
                        <view class="i ico"></view>
                        <text class="span">上传图片</text>
                    </view>
                    <view class="li" v-for="(item, item_index) in ARR" :key="item_index">
                        <image mode="aspectFill" :src="item"></image>

                        <text @tap.stop.prevent="delFn" class="b ico" :data-url="item"></text>
                    </view>
                </view>
            </view>
            <button class="btn5" formType="submit" :style="'width:90%;--color:' + color + ';--rgbcolor:' + rgbcolor" type="button">保存</button>
        </form>
    </view>
</template>

<script>
var t = require('../../res/www.a9a.cn.js');
var app = getApp();
var a = uni.getStorageSync('COOK_U');
export default {
    data() {
        return {
            ic: 0,
            ARR: [],
            color: '',
            rgbcolor: '',
            qid: ''
        };
    },
    onLoad: function (a) {
        var that = this;
        app.globalData.get_config('_ZEAI').then(function (o) {
            that.setData({
                color: o.color,
                rgbcolor: t.c16_rgb(o.color),
                qid: a.qid
            });
            app.globalData.chklogin().then(function (t) {
                t ||
                    uni.showModal({
                        content: rs.msg,
                        confirmColor: that.color,
                        confirmText: '朕知道了',
                        showCancel: false,
                        success: function (t) {
                            uni.navigateBack();
                        }
                    });
            });
        });
    },
    methods: {
        contentFn: function (t) {
            var n = t.detail.value.length;
            if (n > 140) {
                uni.showToast({
                    title: '最多140字',
                    icon: 'none',
                    mask: true
                });
                n = 140;
            }
            this.setData({
                ic: n
            });
        },

        upFn: function () {
            var that = this;
            var n = 6 - parseInt(this.ARR.length);
            if (n <= 0) {
                uni.showToast({
                    title: '最多上传6张图片哦',
                    icon: 'none',
                    mask: true
                });
            } else {
                uni.chooseMedia({
                    count: n,
                    mediaType: ['image'],
                    sizeType: ['compressed'],
                    sourceType: ['album', 'camera'],
                    success: function (n) {
                        for (var a = that.ARR, e = [], o = 0; o < n.tempFiles.length; o++) {
                            e.push(n.tempFiles[o].tempFilePath);
                        }
                        a = a.concat(e);
                        that.setData({
                            ARR: a
                        });
                    }
                });
            }
        },

        delFn: function (n) {
            var a = n.currentTarget.dataset.url;
            var e = t.json_remove(this.ARR, a);
            this.setData({
                ARR: e
            });
        },

        add_submitFn: function (e) {
            var that = this;
            var i = e.detail.value.content;
            if (t.empty(i) || i.length > 140) {
                uni.showToast({
                    title: '亲，该说点什么吧～',
                    icon: 'none',
                    mask: true
                });
            } else {
                uni.showModal({
                    title: '温馨提醒',
                    content: '确定提交么？\n以下情形话题将被删除或封号：\n1．请勿留下任何形式联系方法\n2．请勿发表涉黄涉政相关言论\n3．请勿发表辱骂攻击他人言论',
                    confirmColor: that.color,
                    cancelText: '取消',
                    confirmText: '确定',
                    success: function (e) {
                        if (e.confirm) {
                            var s = that.ARR;
                            var c = s.length;
                            if (c > 0) {
                                var r = Array();
                                var d = 0;
                                !(function e() {
                                    uni.showLoading({
                                        title: '正在上传..第' + parseInt(d + 1),
                                        mask: true
                                    });
                                    uni.uploadFile({
                                        url: app.globalData.WWW + '/api/wxxcx/qun.php',
                                        filePath: s[d],
                                        name: 'pic',
                                        formData: {
                                            submitok: 'trend_add_uppic',
                                            openid: a.openid,
                                            qid: that.qid
                                        },
                                        success: function (s) {
                                            if (1 == (s = t.jsoneval(s.data)).flag) {
                                                r.push(s._s);
                                                {
                                                    d++;
                                                    if (d < c) {
                                                        setTimeout(function () {
                                                            e();
                                                        }, 300);
                                                    } else {
                                                        uni.hideLoading();
                                                        var l = r.join(',');
                                                        app.globalData.ajax({
                                                            url: 'qun.php',
                                                            data: {
                                                                submitok: 'trend_add',
                                                                content: i,
                                                                piclist: l,
                                                                openid: a.openid,
                                                                qid: that.qid
                                                            },
                                                            success: function (t) {
                                                                if (1 == t.flag) {
                                                                    uni.hideLoading(),
                                                                        uni.showToast({
                                                                            title: t.msg,
                                                                            mask: true,
                                                                            duration: 3000
                                                                        }),
                                                                        setTimeout(function () {
                                                                            that.goBack();
                                                                        }, 3000);
                                                                } else {
                                                                    uni.showToast({
                                                                        title: t.msg,
                                                                        icon: 'none',
                                                                        mask: true
                                                                    });
                                                                }
                                                            }
                                                        });
                                                    }
                                                }
                                            } else {
                                                uni.showModal({
                                                    content: s.msg,
                                                    confirmColor: that.color,
                                                    confirmText: '朕知道了',
                                                    showCancel: false,
                                                    success: function (t) {
                                                        uni.navigateBack();
                                                    }
                                                });
                                            }
                                        }
                                    });
                                })();
                            } else {
                                app.globalData.ajax({
                                    url: 'qun.php',
                                    data: {
                                        submitok: 'trend_add',
                                        content: i,
                                        openid: a.openid,
                                        qid: that.qid
                                    },
                                    success: function (t) {
                                        1 == t.flag
                                            ? (uni.showToast({
                                                  title: t.msg,
                                                  mask: true,
                                                  duration: 3000
                                              }),
                                              setTimeout(function () {
                                                  that.goBack();
                                              }, 3000))
                                            : uni.showToast({
                                                  title: t.msg,
                                                  icon: 'none',
                                                  mask: true
                                              });
                                    }
                                });
                            }
                        }
                    }
                });
            }
        },

        goBack: function () {
            var t = getCurrentPages();
            t[t.length - 2].trend_topFn();
            uni.navigateBack({
                delta: 1
            });
        }
    }
};
</script>
<style>
@import 'qun_trend_add.css';
</style>
